Skip to content

Conversation

@knwng
Copy link
Contributor

@knwng knwng commented Aug 15, 2025

Right now if a load op is scalarized, the !alias.scope and !noalias metadata are dropped. This PR is to keep them if exist.

@llvmbot
Copy link
Member

llvmbot commented Aug 15, 2025

@llvm/pr-subscribers-vectorizers

@llvm/pr-subscribers-llvm-transforms

Author: Kyle Wang (knwng)

Changes

Right now if a load op is scalarized, the !alias.scope and !noalias metadata are dropped. This PR is to keep them if exist.


Full diff: https://github.com/llvm/llvm-project/pull/153714.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/VectorCombine.cpp (+12)
diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
index 4a681cbdab8ca..587889873a778 100644
--- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -1811,6 +1811,10 @@ bool VectorCombine::scalarizeLoadExtract(Instruction &I) {
   // erased in the correct order.
   Worklist.push(LI);
 
+  LLVMContext &ctx = LI->getContext();
+  unsigned aliasScopeKind = ctx.getMDKindID("alias.scope");
+  unsigned noAliasKind = ctx.getMDKindID("noalias");
+
   // Replace extracts with narrow scalar loads.
   for (User *U : LI->users()) {
     auto *EI = cast<ExtractElementInst>(U);
@@ -1831,6 +1835,14 @@ bool VectorCombine::scalarizeLoadExtract(Instruction &I) {
         LI->getAlign(), VecTy->getElementType(), Idx, *DL);
     NewLoad->setAlignment(ScalarOpAlignment);
 
+    if (MDNode *aliasScope = LI->getMetadata(aliasScopeKind)) {
+      NewLoad->setMetadata(aliasScopeKind, aliasScope);
+    }
+
+    if (MDNode *noAlias = LI->getMetadata(noAliasKind)) {
+      NewLoad->setMetadata(noAliasKind, noAlias);
+    }
+
     replaceValue(*EI, *NewLoad);
   }
 

@bcahoon
Copy link
Contributor

bcahoon commented Aug 15, 2025

Hi @knwng, thanks! I haven't look at the patch in any detail. It does need a lit test though.

@nikic nikic requested review from RKSimon, davemgreen and fhahn August 15, 2025 09:32
@nikic nikic changed the title Make VectorCombine Pass Alias Info [VectorCombine] Preserve scoped alias metadata Aug 15, 2025

if (MDNode *noAlias = LI->getMetadata(noAliasKind)) {
NewLoad->setMetadata(noAliasKind, noAlias);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, it would be better to use getAAMetadata() and adjustForAccess() here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @nikic, thank you for the advice. I know it's a more thoughtful solution. But I'm not familiar with AA in LLVM. Can you guide me on how to use adjustForAccess(), like how to correctly calculate offset and type? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type is VecTy->getElementType(). The offset would be Idx * the type size like here:

if (auto *C = dyn_cast<ConstantInt>(Idx))
return commonAlignment(VectorAlignment,
C->getZExtValue() * DL.getTypeStoreSize(ScalarType));

The problem is if Idx is a variable. In that case the offset is unknown. So I would make the whole logic conditional on the dyn_cast<ConstantInt>(Idx) case.

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you find this by inspection or was there a real world case that we can create test from please?

@frederik-h
Copy link
Contributor

Hi @knwng, thanks! I haven't look at the patch in any detail. It does need a lit test though.

I have written a test that reproduces the issue: 13fa0d2.
@knwng I hope this helps as a starting point.

@knwng
Copy link
Contributor Author

knwng commented Aug 15, 2025

Did you find this by inspection or was there a real world case that we can create test from please?

It's found in a real world case. @frederik-h 's test is extracted from it.

@github-actions
Copy link

github-actions bot commented Aug 16, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

cl::desc("Disable all vector combine transforms"));
static cl::opt<bool>
DisableVectorCombine("disable-vector-combine", cl::init(false), cl::Hidden,
cl::desc("Disable all vector combine transforms"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not reformat unrelated code (you can use git-clang-format to only format changed lines).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry. Fixed

@knwng
Copy link
Contributor Author

knwng commented Aug 17, 2025

Hi @nikic, is the CI failure a runner issue?

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@frederik-h
Copy link
Contributor

Hi @nikic, is the CI failure a runner issue?

Looks like it is. If you retrigger the run, e.g. after you resolve the conflict, it will probably work.

@frederik-h frederik-h enabled auto-merge (squash) August 18, 2025 16:01
auto-merge was automatically disabled August 18, 2025 16:15

Head branch was pushed to by a user without write access

@nikic nikic enabled auto-merge (squash) August 18, 2025 16:24
@nikic nikic merged commit 064f02d into llvm:main Aug 18, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants